package com.jt.tenant.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetNetData;
import com.jt.tenant.conn.SetWifiSetting;
import com.jt.tenant.dialog.TipDialog;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

/**
 * Created by 马长鸣 on 2016/5/14.
 */
public class WifiControlActivity extends JTActivity implements View.OnClickListener {

    private TextView mTitleName, commit;

    private TextView tv_wifi_used,tv_wifi_overplus;

    private ImageView im_wifi_on;

    private EditText et_wifi_password;

    private String wifi, wifipwd,mWifi,mWifiPwd;

    private TipDialog mDialog;

    private GetNetData getNetData;

    private float used,overplus;

    public static RefreshLinster refreshLinster;

    private SetWifiSetting setWifiSetting = new SetWifiSetting(wifi, wifipwd, new AsyCallBack<SetWifiSetting.Info>() {

        @Override
        public void onSuccess(String toast, int type, SetWifiSetting.Info info) throws Exception {

            super.onSuccess(toast, type, info);

            resultDialog = new TipDialog(context);

            resultDialog.setType(0);

            if (info.msg.equals("000")) {

                resultDialog.setTip("设置成功,最新状态将在一段时间内生效");

            } else {

                resultDialog.setTip(info.msg);

            }
            if (!wifi.equals("A")) {

                JTApplication.jtPreferences.saveWIFI(wifi);
            }

            if (!wifipwd.equals("A")) {

                JTApplication.jtPreferences.saveWIFIPWD(wifipwd);
            }

            resultDialog.show();

        }
    });

    private TipDialog resultDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_wifi_control);

        initView();

    }

    private void initView() {

        (mTitleName = (TextView) findViewById(R.id.tv_title_name)).setText("WIFI管理");

        (im_wifi_on = (ImageView) findViewById(R.id.im_wifi_on)).setOnClickListener(this);

        tv_wifi_used = (TextView)findViewById(R.id.tv_wifi_used);

        tv_wifi_overplus = (TextView)findViewById(R.id.tv_wifi_overplus);

        et_wifi_password = (EditText) findViewById(R.id.et_wifi_password);

        (commit = (TextView) findViewById(R.id.tv_wifi_commit)).setOnClickListener(this);

        commit.setText("提   交");

        wifi = JTApplication.jtPreferences.readWIFI();

        if(wifi.equals("")){

            wifi = "0";

        }

        wifipwd = JTApplication.jtPreferences.readWIFIPWD();

        if(wifipwd.equals("")){

            et_wifi_password.setText("");

        }else {

            et_wifi_password.setText(wifipwd);

        }

        if (wifi.equals("0")) {

            im_wifi_on.setImageResource(R.mipmap.icon_wifi_off);

        } else if (wifi.equals("1")) {

            im_wifi_on.setImageResource(R.mipmap.icon_wifi_on);

        }

        getNetData = new GetNetData(new AsyCallBack<GetNetData.Info>(){

            @Override
            public void onSuccess(String toast, int type, GetNetData.Info info) throws Exception {

                super.onSuccess(toast, type, info);

                if(info.msg.equals("000")){

                    used = Float.parseFloat(info.rate);

                    overplus = Float.parseFloat(info.surplus);

                    if(used>1000){

                        tv_wifi_used.setText(Tools.F_num(String.valueOf(used/1000))+"G");

                    }else {

                        tv_wifi_used.setText(Tools.F_num(String.valueOf(used))+"M");

                    }

                    if(overplus>1000){

                        tv_wifi_overplus.setText(Tools.F_num(String.valueOf(overplus/1000))+"G");

                    }else {

                        tv_wifi_overplus.setText(Tools.F_num(String.valueOf(overplus))+"M");

                    }

                }else {

                    UtilToast.show(WifiControlActivity.this,info.msg);

                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {

                super.onFail(toast, type);

                UtilToast.show(WifiControlActivity.this,toast);

            }
        });

        getNetData.execute(this);

        refreshLinster = new RefreshLinster() {
            @Override
            public void refresh() {

                setWifiSetting.execute(context);

            }
        };



    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.im_wifi_on:

                if (wifi.equals("0")) {

                    im_wifi_on.setImageResource(R.mipmap.icon_wifi_on);

                    wifi = "1";

                } else if (wifi.equals("1")) {

                    im_wifi_on.setImageResource(R.mipmap.icon_wifi_off);

                    wifi = "0";

                }else {

                    im_wifi_on.setImageResource(R.mipmap.icon_wifi_on);

                    wifi = "1";

                }

                break;

            case R.id.tv_wifi_commit:

                onWifiCommit();

                 break;


        }


    }

    public void onWifiCommit() {

        mWifi = JTApplication.jtPreferences.readWIFI();

        mWifiPwd = JTApplication.jtPreferences.readWIFIPWD();

        wifipwd = et_wifi_password.getText().toString().trim();

        if(wifi.equals(mWifi)&&wifipwd.equals(mWifiPwd)){


            UtilToast.show(WifiControlActivity.this,"请修改后提交");


        }
        else if (wifipwd.length()!=8||!Tools.isPassword(wifipwd)){

            UtilToast.show(WifiControlActivity.this,"WIFI密码必须为8位字母或数字");

        }else {

            if (wifi.equals(mWifi)) {

                wifi = "A";

            }

            if (wifipwd.equals(mWifiPwd)) {

                wifipwd = "A";

            }

            mDialog = new TipDialog(context);

            mDialog.setType(8);

            mDialog.setTip("确认更改WIFI状态？");

            mDialog.show();
        }

    }

    public abstract class RefreshLinster{

        public abstract void refresh();

    }

}



